<template>
  <div class="user-management">
    <el-card class="main-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><DataBoard /></el-icon>
            <h2 class="header-title">用户管理：基础数据维护</h2>
          </div>
          <div class="header-right">
            <el-tag type="info" size="large">共 {{ total }} 条记录</el-tag>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-section">
        <el-form :model="searchForm" inline class="search-form">
          <el-form-item>
            <el-input
              placeholder="请输入真实姓名关键字"
              v-model="searchForm.realName"
              clearable
              prefix-icon="Search"
              style="width: 300px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchData" class="action-button">
              <el-icon><Search /></el-icon>搜索用户
            </el-button>
            <el-button type="success" @click="addData" class="action-button">
              <el-icon><Plus /></el-icon>添加用户
            </el-button>
            <el-button @click="reset" class="action-button">
              <el-icon><Refresh /></el-icon>重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域 -->
      <div class="table-section">
        <div class="table-header">
          <h3 class="table-title">
            <el-icon><List /></el-icon>
            用户详细信息
          </h3>
        </div>
        <el-table 
          :data="tableData" 
          border 
          stripe 
          class="data-table"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          v-loading="loading"
        >
          <el-table-column type="index" label="序号" width="80" align="center" />
          <el-table-column prop="id" label="用户ID" width="100" align="center" />
          <el-table-column prop="realName" label="真实姓名" min-width="120" />
          <el-table-column prop="userName" label="系统账号" min-width="120" />
          <el-table-column prop="email" label="邮箱" min-width="180" />
          <el-table-column prop="phoneNumber" label="电话号码" min-width="130" />
          <el-table-column
            prop="gender"
            label="性别"
            width="80"
            align="center"
          >
            <template #default="{ row }">
              <el-tag :type="row.gender === 1 ? 'primary' : 'success'" size="small">
                {{ row.gender === 1 ? '男' : '女' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="role" label="角色编号" width="100" align="center" />
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template #default="{ row }">
              <el-tag :type="row.status === 1 ? 'success' : 'danger'" size="small">
                {{ row.status === 1 ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="isDeleted"
            label="逻辑删除"
            width="100"
            align="center"
          >
            <template #default="{ row }">
              <el-tag :type="row.isDeleted ? 'danger' : 'success'" size="small">
                {{ row.isDeleted ? '已删除' : '正常' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180" align="center">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="editData(row)" class="table-button">
                <el-icon><Edit /></el-icon>修改
              </el-button>
              <el-button type="danger" size="small" @click="deleteData(row.id)" class="table-button">
                <el-icon><Delete /></el-icon>删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页区域 -->
      <div class="pagination-section">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        />
      </div>
    </el-card>

    <!-- 抽屉弹窗 -->
    <el-drawer 
      v-model="drawerVisible" 
      :title="title" 
      size="40%"
      direction="rtl"
      class="user-drawer"
    >
      <div class="drawer-content">
        <el-form :model="formData" ref="userForm" label-width="100px" class="drawer-form">
          <el-form-item label="真实姓名" required>
            <el-input v-model="formData.realName" placeholder="请输入真实姓名" />
          </el-form-item>
          <el-form-item label="系统账号" required>
            <el-input v-model="formData.userName" placeholder="请输入系统账号" />
          </el-form-item>
          <el-form-item label="密码" required>
            <el-input type="password" v-model="formData.password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="formData.email" placeholder="请输入邮箱地址" />
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="formData.phoneNumber" placeholder="请输入电话号码" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="formData.gender">
              <el-radio :value="0">女</el-radio>
              <el-radio :value="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="角色编号">
            <el-input v-model="formData.role" placeholder="请输入角色编号" />
          </el-form-item>
          <el-form-item label="状态">
            <el-switch 
              v-model="formData.status" 
              :active-value="1" 
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
            />
          </el-form-item>
          <el-form-item label="逻辑删除">
            <el-switch 
              v-model="formData.isDeleted" 
              active-text="已删除"
              inactive-text="正常"
            />
          </el-form-item>
          <el-form-item class="drawer-actions">
            <el-button type="primary" @click="submitData" class="submit-button">
              <el-icon><Setting /></el-icon>{{ title }}
            </el-button>
            <el-button @click="drawerVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, reactive, watch, inject } from 'vue';
import { useRouter } from 'vue-router';
import qs from 'qs';
// 导入 Element Plus 图标
import {
  Delete,
  Edit,
  User,
  DataBoard,
  Setting,
  Search,
  Plus,
  Refresh,
  List
} from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const axios = inject('axios');
const router = useRouter();

const searchForm = reactive({ realName: '' });
const tableData = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const loading = ref(false);

const drawerVisible = ref(false);
const title = ref('');
const formData = reactive({
  id: null,
  realName: '',
  userName: '',
  password: '',
  email: '',
  phoneNumber: '',
  gender: 0,
  role: 1,
  status: 1,
  isDeleted: false,
});

watch([currentPage, pageSize], loadUserData);

function loadUserData() {
  loading.value = true;
  axios.post('UserController/queryUserByName', qs.stringify({
    cur: currentPage.value,
    number: pageSize.value,
    realName: searchForm.realName
  })).then(response => {
    let rb = response.data;
    if (rb.status === 200) {
      tableData.value = rb.data;
      total.value = rb.total;
    } else {
      tableData.value = [];
      total.value = 0;
      ElMessage.warning('暂无数据');
    }
  }).catch(error => {
    console.error('加载数据失败:', error);
    ElMessage.error('加载数据失败');
  }).finally(() => {
    loading.value = false;
  });
}

loadUserData();

function reset() {
  searchForm.realName = '';
  currentPage.value = 1;
  loadUserData();
  ElMessage.success('已重置搜索条件');
}

function searchData() {
  currentPage.value = 1;
  loadUserData();
}

function addData() {
  title.value = '添加用户';
  drawerVisible.value = true;
  Object.assign(formData, {
    id: null,
    realName: '',
    userName: '',
    password: '',
    email: '',
    phoneNumber: '',
    gender: 0,
    role: 1,
    status: 1,
    isDeleted: false,
  });
}

function editData(row) {
  title.value = '修改用户';
  drawerVisible.value = true;
  Object.assign(formData, { ...row });
}

function deleteData(id) {
  ElMessageBox.confirm('确定要删除这个用户吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    // 这里添加删除逻辑
    ElMessage.success('删除成功');
    loadUserData();
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
}

function submitData() {
  // 这里添加提交逻辑
  ElMessage.success(title.value + '成功');
  drawerVisible.value = false;
  loadUserData();
}
</script>

<style scoped>
.user-management {
  min-height: 100%;
}

.main-card {
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  color: #409EFF;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
}

.search-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.search-form {
  margin: 0;
}

.action-button {
  margin-right: 8px;
  border-radius: 6px;
  font-weight: 500;
}

.table-section {
  margin-bottom: 24px;
}

.table-header {
  margin-bottom: 16px;
}

.table-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.data-table {
  border-radius: 8px;
  overflow: hidden;
}

.data-table ::v-deep .el-table__header {
  background: #f5f7fa;
}

.data-table ::v-deep .el-table__row:hover {
  background-color: #f0f9ff !important;
}

.table-button {
  margin-right: 4px;
  border-radius: 4px;
}

.pagination-section {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

.user-drawer ::v-deep .el-drawer__header {
  background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
  color: white;
  margin-bottom: 0;
  padding: 20px 24px;
}

.user-drawer ::v-deep .el-drawer__title {
  color: white;
  font-weight: 600;
}

.drawer-content {
  padding: 24px;
}

.drawer-form {
  max-width: 100%;
}

.drawer-form ::v-deep .el-form-item__label {
  font-weight: 500;
  color: #606266;
}

.drawer-actions {
  margin-top: 32px;
  text-align: center;
}

.submit-button {
  margin-right: 12px;
  padding: 12px 24px;
  border-radius: 6px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-form {
    flex-direction: column;
  }
  
  .search-form .el-form-item {
    margin-bottom: 12px;
  }
  
  .action-button {
    margin-bottom: 8px;
  }
  
  .card-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
}
</style>
